<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        (function () {
            var s = {
                s: 'student',
                getS: function () {
                    console.log(this.s);
                }
            };

            var t = {
                s: 'teacher'
            };

            var getS = s.getS;
            var getS1 = getS.bind(s);

            s.getS(); //student
            s.getS.apply(t); //teacher
            getS(); //s对象 
            getS1.call(t); //student 想不明白。。。
        })();

        (function () {
            function switchCase(value) {
                switch (value) {
                    case '0':
                        console.log("case 0")

                    case '1':
                        console.log("case 1")
                        break;

                    case undefined:
                        console.log("undefined")
                        break;

                    default:
                        console.log('deault')
                        break;
                }
            }

            switchCase(0); //case 0,case 1 错误 应该是default
            switchCase('0'); //case 0 ,case 1
            switchCase(); //undefined
        })();

        (function () {
            function randomArrayIndex(array, item) {
                var oldArray = array;
                var length = array.length;
                var max = length - 1;
                var nochangItem = item;
                var newArray = [];
                var index = findIndex(array, item);
                for (let i = 0; i < max; i++) {
                    var temp = Math.floor(Math.random() * max);

                    var isHave = isExist(newArray, temp);
                    pushIndex();

                    function pushIndex() {
                        if (isHave) {
                            temp = Math.floor(Math.random() * max + 1);
                            isHave = isExist(newArray, temp);
                            pushIndex();
                        } else {
                            newArray.push(temp);
                            console.log(temp);
                        }
                    }
                }
                console.log(oldArray)
                console.log(newArray);

                var b = [];
                for (let i = 0; i < max; i++) {
                    b[i] = oldArray[newArray[i]];
                }

                console.log(b);
                b.splice(index, 0, nochangItem);
                console.log(b);


                function isExist(array, item) {
                    if (item === index) {
                        return true
                    } else {
                        if (array.length) {
                            for (let i = 0, len = array.length; i < len; i++) {
                                if (array[i] === item) {
                                    return true
                                }
                            }
                            return false;
                        } else {
                            return false;
                        }
                    }
                }

                function findIndex(array, item) {
                    for (let i = 0, len = array.length; i < len; i++) {
                        if (array[i] === item) {
                            return i;
                        }
                    }
                }

                return newArray;

            }

            var a = [1, 2, 3, 4, 5, 6, 7];
            //randomArrayIndex(a, a[2]);
        })();

        //可以指定一个数组的某一个元素的位置不变，而其他元素的位置随机改变
        (function () {
            function arrayRandom(array, index2) {
                array.map(function (item, index) {
                    if (index !== index2) {
                        var x = parseInt(Math.random() * array.length);
                        if (x !== index2) {
                            array[index] = array[x];
                            array[x] = item
                        }
                    }
                })
                console.log(array)
            }
            var a = [1, 2, 3, 4, 5, 6, 7];
            console.log(a)
            arrayRandom(a, 6);
        })();
    </script>
</body>

</html>